﻿using System;
using Entities.DataBase.Persistent;
using Entities.Enumeration;

namespace Entities.DataBase.Obj {

    public class OrderUpdator {

        public double? Postage      { get; set; }
        public double? Price        { get; set; }
        public int?    CourierId    { get; set; }
        public string  TrackingId   { get; set; }
        public string  TrackingData { get; set; }

        public void Update(Order order) {
            order.DateUpdated = DateTime.UtcNow;
            if (CourierId != null && order.CourierId != (int) CourierId) {
                order.CourierId = (int)CourierId;
            }

            if (Postage != null) {
                var current = order.OrderPostage;
                order.OrderPostage = (decimal) Postage;
                order.TotalPostage = order.TotalPostage - current + (decimal)Postage;
            }

            if (Price != null && Price > 0) {
                order.Price = (decimal) Price;
            }

            if (!string.IsNullOrEmpty(TrackingId) && !order.TrackingId.Equals(TrackingId)) {
                 order.TrackingId = TrackingId;
                 order.Status = OrderStatus.Shipping;
            }
        }

    }
}